#include"TextureSection.h"

TextureSection::TextureSection()
{
	this->bottom = 0.0f;
	this->left = 0.0f;
	this->right = 0.0f;
	this->top = 0.0f;
	this->width = 0.0f;
	this->height = 0.0f;
	this->id = 0;
	this->TextureId = 0;
}

TextureSection::TextureSection(const TextureSection& rhs)
{
	this->bottom = rhs.bottom;
	this->left = rhs.left;
	this->right = rhs.right;
	this->top = rhs.top;
	this->id = rhs.id;
	this->TextureId = rhs.TextureId;
	this->width = rhs.width;
	this->height = rhs.height;
}

TextureSection::TextureSection(Texture* texture, int new_id)
{
	this->id = new_id;
	this->TextureId = texture->GetId();
	this->left = 0.0f;
	this->right = 1.0f;
	this->top = 0.0f;
	this->bottom = 1.0f;
	this->width = texture->GetWidth();
	this->height = texture->GetHeight();
}

TextureSection::TextureSection(Texture* texture, int new_id, float left,float top,float bottom,float right)
{
	this->TextureId = texture->GetId();
	this->id = new_id;
	this->left = left;
	this->right = right;
	this->top = top;
	this->bottom = bottom;
	this->width = texture->GetWidth() * (this->right - this->left);
	this->height = texture->GetHeight() * (this->bottom - this->top);
}

unsigned int TextureSection::MakeTextureSection(Texture* texture, int new_id, float left,float top,float bottom,float right)
{
	this->TextureId = texture->GetId();
	this->id = new_id;
	this->left = left;
	this->right = right;
	this->top = top;
	this->bottom = bottom;
	this->width = texture->GetWidth() * (this->right - this->left);
	this->height = texture->GetHeight() * (this->bottom - this->top);
	return new_id;
}


bool TextureSection::DrawTextureSection(int X, int Y, GLfloat Scale,GLfloat Rotation, GLfloat red, GLfloat green, GLfloat blue)
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//glLoadIdentity();

	
	float x1, y1;
	float x2, y2;
	float x3, y3;
	float x4, y4;

	float x1_r, y1_r;
	float x2_r, y2_r;
	float x3_r, y3_r;
	float x4_r, y4_r;

	x1 = - ((float) this->width) * Scale / 2.0; 
	y1 = - ((float) this->height) * Scale / 2.0;

	x2 = + ((float) this->width) * Scale / 2.0; 
	y2 = - ((float) this->height) * Scale / 2.0;

	x3 = + ((float) this->width) * Scale / 2.0; 
	y3 = + ((float) this->height) * Scale / 2.0;

	x4 = - ((float) this->width) * Scale / 2.0; 
	y4 = + ((float) this->height) * Scale / 2.0;

	x1_r = -sin(Rotation) * y1 + cos(Rotation) * x1;
	y1_r = cos(Rotation) * y1 + sin(Rotation) * x1;

	x2_r = -sin(Rotation) * y2 + cos(Rotation) * x2;
	y2_r = cos(Rotation) * y2 + sin(Rotation) * x2;

	x3_r = -sin(Rotation) * y3 + cos(Rotation) * x3;
	y3_r = cos(Rotation) * y3 + sin(Rotation) * x3;

	x4_r = -sin(Rotation) * y4 + cos(Rotation) * x4;
	y4_r = cos(Rotation) * y4 + sin(Rotation) * x4;

	glBindTexture(GL_TEXTURE_2D, this->id);

	glBegin(GL_QUADS);
		glTexCoord2f(this->left, this->top); glColor3f(red, green, blue); 	glVertex2f(X + x1_r, Y + y1_r);
		glTexCoord2f(this->right, this->top); glColor3f(red, green, blue); 	glVertex2f(X + x2_r, Y + y2_r);
		glTexCoord2f(this->right, this->bottom); glColor3f(red, green, blue); 	glVertex2f(X + x3_r, Y + y3_r);
		glTexCoord2f(this->left, this->bottom); glColor3f(red, green, blue); 	glVertex2f(X + x4_r, Y + y4_r);
	glEnd();

	return true;
}

TextureSection::~TextureSection()
{
	this->bottom = 0.0f;
	this->left = 0.0f;
	this->right = 0.0f;
	this->top = 0.0f;
	this->id = 0;
	this->TextureId = 0;
}